package com.example.mybmicalculator;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mybmicalculator.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    int chooseButton = 0;
    double BMI;
    String text = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initData();
    }

    private void initData() {

//        如果选择男生1，选择女2
        binding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.rbtn_man) {
                    chooseButton = 1;
                } else {
                    chooseButton = 2;
                }
            }
        });


//        点击事件
        binding.btnCalculate.setOnClickListener(view -> {


//            判断用户是否输入数值
            if (TextUtils.isEmpty(binding.etWeight.getText().toString().trim())
                    || TextUtils.isEmpty(binding.etHeight.getText().toString().trim())
                    || chooseButton == 0) {

                Toast.makeText(this, "请输入数值", Toast.LENGTH_SHORT).show();

            } else {
//                拿到用户输入的数据
                String tz = binding.etWeight.getText().toString().trim();
                String sz = binding.etHeight.getText().toString().trim();

//                先转double
                BMI = Math.round(Double.parseDouble(tz) / Math.pow(Double.parseDouble(sz), 2));
//                选择了男生
                if (chooseButton == 1) {
                    if (BMI < 20) {
                        text = "体重过轻";
                    } else if (BMI < 25) {
                        text = "体重正常";
                    } else if (BMI < 27) {
                        text = "体重超重";
                    } else if (BMI < 30) {
                        text = "轻度肥胖";
                    } else if (BMI < 35) {
                        text = "中度肥胖";
                    } else if (BMI > 35) {
                        text = "重度肥胖";
                    }

                    binding.tvBMI.setText(BMI + "");
                    binding.tvDiagnosis.setText(text);
                }
//选择了女生
                if (chooseButton == 2) {
                    if (BMI < 19) {
                        text = "体重过轻";
                    } else if (BMI < 24) {
                        text = "体重正常";
                    } else if (BMI < 26) {
                        text = "体重超重";
                    } else if (BMI < 29) {
                        text = "轻度肥胖";
                    } else if (BMI < 34) {
                        text = "中度肥胖";
                    } else if (BMI > 34) {
                        text = "重度肥胖";
                    }

                    binding.tvBMI.setText(BMI + "");
                    binding.tvDiagnosis.setText(text);
                }
            }
        });
    }
}
